package com.shjn.squirrel.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.shjn.squirrel.R;
import com.shjn.squirrel.application.MyApplication;
import com.shjn.squirrel.bean.storageBean.WastePrice;
import com.shjn.squirrel.customView.BackAndTimerView;
import com.shjn.squirrel.model.Constant;
import com.shjn.squirrel.model.LocalStorage;
import com.shjn.squirrel.model.UserInfoCache;
import com.shjn.squirrel.utils.SoundPlayUtils;

import java.util.ArrayList;

/**
 * 用户投递一次或者多次同种类的垃圾的显示记录
 */
public class UserRecordOnceFragment extends BaseFragment implements View.OnClickListener {

    private static UserRecordOnceFragment instance;
    private ImageView mImgCategory;
    private TextView mTvQuantity;
    private TextView mTvValue;
    private Button mBtnOVer;
    private Button mBtnAgain;
    private ArrayList<WastePrice> mWastePrice;

    public UserRecordOnceFragment() {
    }

    public synchronized static UserRecordOnceFragment getInstance() {
        if (instance == null) {
            instance = new UserRecordOnceFragment();
        }
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return createView(inflater, R.layout.user_deliver_record_once, container);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initTimer();
        initView();
    }

    /**
     * 初始化倒计时器
     */
    private void initTimer() {
        //加载定时器面板，设置定时时间
        mActivity.mBackAndTimer.setOnBackListener(null).setBackVisibleState(false)
                .setOnTimerFinishListener(new BackAndTimerView.OnTimerFinishListener() {
                    @Override
                    public void onTimerFinish() {
                        //跳转到结束投递页
                        if (!UserFinishFragment.getInstance().isAdded()) {
                            mActivity.mBackAndTimer.stop();
                            SoundPlayUtils.play(16);//感谢使用提示音
                            mActivity.getFragmentManager().beginTransaction()
                                    .replace(R.id.mid, UserFinishFragment.getInstance())
                                    .commit();
                        }
                    }
                }).setTime(60).setVisibleState(true).start();
    }

    /**
     * 初始化相关控件
     */
    private void initView() {
        mImgCategory = (ImageView) mView.findViewById(R.id.img_catogry);
        mTvQuantity = (TextView) mView.findViewById(R.id.tv_quantity);
        mTvValue = (TextView) mView.findViewById(R.id.tv_value);
        mBtnOVer = (Button) mView.findViewById(R.id.btn_over);
        mBtnAgain = (Button) mView.findViewById(R.id.btn_again);
        mBtnOVer.setOnClickListener(this);
        mBtnAgain.setOnClickListener(this);

        mImgCategory.setImageResource(Constant.categoryMap.get(UserInfoCache.category)[1]);
        SpannableString spannableString = null;
        mWastePrice = LocalStorage.getWastePrice(MyApplication.getInstance());

        if (UserInfoCache.category.equals(Constant.bottle)) {
            spannableString = new SpannableString(UserInfoCache.category + " " +
                    String.format("%.0f", UserInfoCache.records.get(UserInfoCache.category).get(1)) + Constant.quantity_unit);

            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#268EF1")),
                    spannableString.toString().indexOf(" ") + 1, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            mTvQuantity.setText(spannableString);

            spannableString = new SpannableString(Constant.get_money + " " + String.format("%.2f", UserInfoCache.records.get(UserInfoCache.category).get(1) * mWastePrice.get(Constant.categoryMap.get(UserInfoCache.category)[3] - 1).getUserPrice()));
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#258DF0")),
                    spannableString.toString().indexOf(" ") + 1, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            mTvValue.setText(spannableString);
        } else if (UserInfoCache.category.equals(Constant.glass) || UserInfoCache.category.equals(Constant.harm)) {
            spannableString = new SpannableString(UserInfoCache.category + " " +
                    String.format("%.0f", UserInfoCache.records.get(UserInfoCache.category).get(1)) + Constant.count_unit);

            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#268EF1")),
                    spannableString.toString().indexOf(" ") + 1, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            mTvQuantity.setText(spannableString);

            spannableString = new SpannableString(Constant.get_integral + " " + String.format("%.0f", UserInfoCache.records.get(UserInfoCache.category).get(1) * mWastePrice.get(Constant.categoryMap.get(UserInfoCache.category)[3] - 1).getUserPrice()));
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#258DF0")),
                    spannableString.toString().indexOf(" ") + 1, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            mTvValue.setText(spannableString);
        } else {
            spannableString = new SpannableString(UserInfoCache.category + " " +
                    String.format("%.2f", UserInfoCache.records.get(UserInfoCache.category).get(0)) + Constant.weight_unit);

            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#268EF1")),
                    spannableString.toString().indexOf(" ") + 1, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            mTvQuantity.setText(spannableString);

            spannableString = new SpannableString(Constant.get_money + " " + String.format("%.2f", UserInfoCache.records.get(UserInfoCache.category).get(0) * mWastePrice.get(Constant.categoryMap.get(UserInfoCache.category)[3] - 1).getUserPrice()));
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#258DF0")),
                    spannableString.toString().indexOf(" ") + 1, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            mTvValue.setText(spannableString);
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //结束投递
            case R.id.btn_over:
                //跳转到结束投递页
                if (!UserFinishFragment.getInstance().isAdded()) {
                    mActivity.mBackAndTimer.stop();
                    SoundPlayUtils.play(16);//感谢使用提示音
                    mActivity.getFragmentManager().beginTransaction()
                            .replace(R.id.mid, UserFinishFragment.getInstance())
                            .commit();
                }
                break;
            //继续投递
            case R.id.btn_again:
                //回到首页
                if (!UserSelectFragment.getInstance().isAdded()) {
                    mActivity.mBackAndTimer.stop();
                    SoundPlayUtils.play(15);//选择垃圾类型提示音
                    mActivity.mBackAndTimer.setVisibleState(false);
                    mActivity.getFragmentManager().beginTransaction()
                            .replace(R.id.mid, UserSelectFragment.getInstance())
                            .commit();
                }
                break;
            default:
                break;
        }
    }
}
